<template>
  <div class="app-container">
    <el-row :gutter="20">

      <!--数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="应用名称" prop="appName">
            <el-input
              v-model="queryParams.appName"
              placeholder="请输入用户名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">

            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >申请</el-button>
            </el-col>

          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
          <!--<right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>-->
        </el-row>


        <el-table v-loading="loading" :data="appList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="编号" align="center" key="appId" prop="appId"  width="50"/>
          <el-table-column label="应用名称" align="center" key="appName" prop="appName" width="230" />
          <el-table-column label="用户名称" align="center" key="nickName" prop="nickName" width="90"/>
          <el-table-column label="简介" align="center" key="text" prop="text" />
          <el-table-column label="我的key值" align="center" key="appKey" prop="appKey" />
          <el-table-column label="创建时间" align="center" prop="createTime"  width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>

          <!--<el-table-column-->
            <!--label="操作"-->
            <!--align="center"-->
            <!--width="160"-->
            <!--class-name="small-padding fixed-width"-->
          <!--&gt;-->
            <!--<template slot-scope="scope" >-->
              <!--<el-col :span="1.5" style="width:140px" v-if="scope.row.status === 0">-->
                <!--<el-button-->
                  <!--type="primary"-->
                  <!--plain-->
                  <!--icon="el-icon-plus"-->
                  <!--size="mini"-->
                  <!--@click="handleApplyKey(scope.row)"-->
                <!--&gt;申请</el-button>-->
              <!--</el-col>-->
              <!--<el-col :span="1.5" style="width:140px" v-else>-->
                <!--已申请-->
              <!--</el-col>-->
            <!--</template>-->
          <!--</el-table-column>-->

        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用名称" prop="appName">
              <el-input v-model="form.appName" placeholder="请输入应用名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="key值">
            <el-input v-model="form.appKey" disabled="disable"></el-input>
          </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="简介" prop="text">
              <el-input v-model="form.text" placeholder="请输入简介" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import { listApp,applyKey,getApp,addApp,editApp,delApp} from "@/api/data/app";
  import { getToken } from "@/utils/auth";
  import { setAccessToken ,getAccessToken} from '@/utils/auth'

  export default {
    name: "Data",
    dicts: ['sys_normal_disable'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        appList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          appName: undefined
        },
        // 表单校验
        rules: {
          appName: [
            { required: true, message: "应用名称不能为空", trigger: "blur" }
          ]
        }
      };
    },
    watch: {

    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询用户列表 */
      getList() {
        this.loading = true;
        listApp(this.queryParams).then(response => {
            this.appList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      /** 申请按钮操作 */
      // handleApplyKey(row) {
      //   const appId = row.id;
      //   applyKey(appId).then(response => {
      //     // setAccessToken(response.accessToken);
      //     this.getList();
      //   });
      // },

      // 表单重置
      reset() {
        this.form = {
          appName: undefined,
          text :undefined,
          applyKey :undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.appId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
          this.reset();
          this.open = true;
          this.title = "申请应用";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.appId || this.ids;
        getApp(id).then(response => {
          this.form = response.dataApp;
          this.open = true;
          this.title = "修改应用";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.appId != undefined) {
              editApp(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addApp(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.appId || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function() {
          return delApp(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },


    }
  };
</script>
